<?php
/**
 * Created by PhpStorm.
 * User: li gui gong
 * Date: 2017/5/1
 * Time: 16:34
 */
namespace Home\Model;
use Think\Model;
class PeopleModel extends Model{
    public function search($pageSize = 10)
    {
        /**************************************** 搜索 ****************************************/
        $where = array();
        //姓名搜索
        if($name = I('get.name'))
            $where['a.name'] = array('like', "%$name%");
        //身份证搜索
        if($idcard = I('get.idcard'))
            $where['a.idcard'] = array('eq', "$idcard");
        //学历查询
        if($education = I('get.education'))
            $where['a.education'] = array('eq', "$education");
        //性别查询
        if($sex = I('get.sex'))
            $where['a.sex'] = array('eq', "$sex");

        /**************************** 翻页 **************************/
        $count = $this->alias('a')->where($where)->count();
        $page = new \Think\Page($count, $pageSize);
        // 配置翻页的样式
        // 修改分页样式
        $page->setConfig('header', '共%TOTAL_ROW%条数据，当前第%NOW_PAGE%/%TOTAL_PAGE%页');
        $page->setConfig('prev', '上一页');
        $page->setConfig('first', '首页');
        $page->setConfig('last',   '尾页');
        $page->setConfig('next', '下一页');
        $page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE%  %LINK_PAGE%  %DOWN_PAGE% %END%');
        $data['page'] = $page->show();
        /************************************** 取数据 ******************************************/
        //SELECT art_id,title,pubtime,is_display,c.username,b.catname from bk_article a LEFT JOIN  bk_cat b ON a.cat_id=b.cat_id LEFT JOIN bk_admin c ON c.id=a.user_id

        $data['data'] = $this->where($where)->alias('a')->limit($page->firstRow.','.$page->listRows)->select();


        //$data['data'] = $this->field('a.*,GROUP_CONCAT(c.role_name) role_name')->alias('a')->where($where)->join('left join bk_admin_role b on a.id=b.admin_id left join bk_role c on c.id=b.role_id')->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
        $data['count'] = $count;
        return $data;
    }

    // 添加前
    protected function _before_insert(&$data, $option)
    {
        $arr = session('adminUser');
        $data['user_id'] = $arr['id'];
        $data['pubtime'] = time();
        $data['lastup'] = time();
        if(isset($_FILES['pic'])&&$_FILES['pic']['error']==0){
            $ret = uploadOne('pic','Article',array(array(480,300),array(207,120)));
            if($ret['ok']==1){
                $data['pic'] = $ret['images'][0];
                $data['thumb'] = $ret['images'][1];
                $data['min_pic'] = $ret['images'][2];
            }else{
                $this->error = $ret['error'];
                return false;
            }
        }
        //判断分类选择了没有
        if($data['cat_id']==0){
            $this->error = '请选择分类';
            return false;
        }
    }
    //添加后(给分类下的数量增加1)
    protected function _after_insert($data, $options){
        $cat_id = $data['cat_id'];
        $model = M('Cat');
        $model->where('cat_id='.$cat_id)->setInc('num',1); // 该分类下的数量加1
    }
    // 修改前
    protected function _before_update(&$data, $option)//data中没有隐藏域提交过来的信息
    {
        if(isset($_FILES['pic'])&&$_FILES['pic']['error']==0){
            $ret = uploadOne('pic','Article',array(array(480,300),array(207,120)));
            if($ret['ok']==1){
                $data['pic'] = $ret['images'][0];
                $data['thumb'] = $ret['images'][1];
                $data['min_pic'] = $ret['images'][2];
            }else{
                $this->error = $ret['error'];
                return false;
            }
            //如果上传了图片就把原来的删除掉
            deleteImage(array(
                I('post.old_pic'),
                I('post.old_thumb'),
                I('post.old_min_pic'),
            ));
        }
        //判断分类选择了没有
        if($data['cat_id']==0){
            $this->error = '请选择分类';
            return false;
        }
    }
    // 删除前
    protected function _before_delete($option)//option中有要删的id,表名,模型名
    {
        if(is_array($option['where']['art_id']))
        {
            $this->error = '不支持批量删除';
            return FALSE;
        }
        $images = $this->field('pic,thumb,min_pic')->find($option['where']['art_id']);
        deleteImage($images);//传一个一维数组
    }
}



